package com.cy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @ClassName RestConsumerController
 * @Description:类描述
 * @Author Tedu
 * @Date 2021/5/31 16:59
 * @Version V1.0
 **/
@RestController
@RequestMapping("/consumer/template/")
public class RestConsumerController {

    @Autowired
    private  RestTemplate loadBalancedRestTemplate;


    @DeleteMapping("{id}")
    public String doDelete(@PathVariable Integer id){
        String url =
                String.format("http://%s/provider/template/%s", "nacos-provider",id);

        //通过服务提供方执行删除操作"http://
        loadBalancedRestTemplate.delete(url);
        return "delete ok";
    }

    @PostMapping
    public  Map<String,Object> doPost(@RequestBody Map<String, Object> map){
        String url=
                String.format("http://%s/provider/template/", "nacos-provider");
        return loadBalancedRestTemplate.postForObject(url, map, Map.class);
    }

    @PutMapping
    public String doPut(@RequestBody Map<String,Object> map){
        String url=String.format("http://%s/provider/template/", "nacos-provider");
        loadBalancedRestTemplate.put(url, map);
        return "put ok";
    }
}
